package com.css.dzj.dzjportal.modules.jgaction.checkAction.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.css.dzj.dzjportal.common.util.R;
import com.css.dzj.dzjportal.common.util.excel.ExcelData;
import com.css.dzj.dzjportal.common.util.excel.ExportExcelUtils;
import com.css.dzj.dzjportal.common.util.http.HttpClientUtil;
import com.css.dzj.dzjportal.modules.jgaction.checkAction.vo.ApJgxwCheckAction;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.PropertySource;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/getAction")
@PropertySource(value = "classpath:base.properties")
public class CheckActionController {
    @Autowired
    private HttpServletRequest request;
    @Value("${AP_URL}")
    private String AP_URL;

    /***
     * 分页查询检查行为
     * @param superviseItemImplementCode
     * @param checkActionName
     * @param checkForm
     * @param checkType
     * @param checkMode
     * @param pageNo
     * @param pageSize
     * @return
     */
    @GetMapping("/getcheck")
    public R getCheckAction(@RequestParam(required = false) String superviseItemImplementCode,
                            @RequestParam(required = false) String checkActionName,
                            @RequestParam(required = false) String checkResult,
                            @RequestParam(required = false) String checkForm,
                            @RequestParam(required = false) String checkType,
                            @RequestParam(required = false) String checkMode,
                            @RequestParam(required = false) String checkDate,
                            @RequestParam(required = false) Integer pageNo,
                            @RequestParam(required = false) Integer pageSize) {
        HashMap<String, String> map = new HashMap<>();
        map.put("superviseItemImplementCode", superviseItemImplementCode);
        map.put("checkActionName", checkActionName);
        map.put("checkResult", checkResult);
        map.put("checkForm", checkForm);
        map.put("checkType", checkType);
        map.put("checkMode", checkMode);
        map.put("checkDate", checkDate);
        map.put("pageNo", pageNo.toString());
        map.put("pageSize", pageSize.toString());
        String msg = HttpClientUtil.doGet(AP_URL + "/check/getAll", map);
        JSONObject object = JSON.parseObject(msg);
        return R.ok().put("data", object);
    }

    /**
     * 查看检查行为详情
     *
     * @param actionId
     * @return
     */
    @GetMapping("/getById")
    public R getById(String actionId) {
        String msg = HttpClientUtil.doGet(AP_URL + "/check/getCheckById?actionId=" + actionId);
        JSONObject object = JSON.parseObject(msg);
        return R.ok().put("data", object);
    }

    /***
     * 获取检查行为总数
     * @return
     */
    @GetMapping("/getcount")
    public R getCount() {
        String msg = HttpClientUtil.doGet(AP_URL + "/check/num");
        Object object = JSON.parse(msg);
        return R.ok().put("data", object);
    }

    /**
     * 导出全部
     * @param superviseItemImplementCode
     * @param checkActionName
     * @param checkActionCode
     * @param checkForm
     * @param checkType
     * @param checkMode
     * @param response
     * @return
     * @throws ParseException
     */
    @GetMapping("/exportAll")
    public R exportAll(@RequestParam(required = false) String superviseItemImplementCode,
                       @RequestParam(required = false) String checkActionName,
                       @RequestParam(required = false) String checkActionCode,
                       @RequestParam(required = false) String checkForm,
                       @RequestParam(required = false) String checkType,
                       @RequestParam(required = false) String checkMode,
                       @RequestParam(required = false) String checkDate,
                       HttpServletResponse response) throws ParseException {
        HashMap<String, String> map = new HashMap<>();
        map.put("superviseItemImplementCode", superviseItemImplementCode);
        map.put("checkActionName", checkActionName);
        map.put("checkActionCode", checkActionCode);
        map.put("checkForm", checkForm);
        map.put("checkType", checkType);
        map.put("checkMode", checkMode);
        map.put("checkDate", checkDate);
        String msg = HttpClientUtil.doGet(AP_URL + "/check/getList", map);
        List<ApJgxwCheckAction> list = JSON.parseArray(msg, ApJgxwCheckAction.class);
        export(response,list);
        return R.ok();
    }

    /**
     * 通过ids导出选中数据
     * @param response
     * @param ids
     * @return
     * @throws ParseException
     */
    @GetMapping("/exportByIds")
    public R exportByIds(HttpServletResponse response, String ids){
        String msg = HttpClientUtil.doGet(AP_URL + "/check/getByIds?ids="+ids);
        List<ApJgxwCheckAction> list=JSON.parseArray(msg, ApJgxwCheckAction.class);
        export(response,list);
        return R.ok();
    }

    /***
     * 导出逻辑
     * @param response
     * @param list
     */
    private void export(HttpServletResponse response, List<ApJgxwCheckAction> list) {
        if (list == null || list.size() < 0) {
            throw new RuntimeException("节假日为空");
        }
        // 创建导出实体
        ExcelData data = new ExcelData();
        data.setName("行政检查行为监管表");
        /**
         * 设置Excel标题
         */
        ArrayList<String> titles = new ArrayList<>();
        titles.add("记录唯一标识");
        titles.add("监管事项目录编码");
        titles.add("监管事项检查实施清单编码");
        titles.add("检查行为名称");
        titles.add("检查行为编号");
        titles.add("实施机构");
        titles.add("检查类别");
        titles.add("检查方式");
        titles.add("检查结果");
        titles.add("检查时间");
        data.setTitles(titles);
        /**
         * 处理行数据
         */
        ArrayList<List<Object>> rows = new ArrayList<>();
        for (ApJgxwCheckAction apJgxwCheckAction : list) {
            ArrayList<Object> row = new ArrayList<>();
            row.add(apJgxwCheckAction.getRecordUniqueIdentity());
            row.add(apJgxwCheckAction.getSuperviseItemImplementCode());
            row.add(apJgxwCheckAction.getSuperviseItemCheckIcode());
            row.add(apJgxwCheckAction.getCheckActionName());
            row.add(apJgxwCheckAction.getCheckActionCode());
            row.add(apJgxwCheckAction.getImplementInstitution());
            if (apJgxwCheckAction.getCheckType().equals("1")) {
                row.add("现场检查");
            }else if(apJgxwCheckAction.getCheckType().equals("2")) {
                row.add("非现场检查");
            }
            if (apJgxwCheckAction.getCheckMode().equals("1")) {
                row.add("“双随机、一公开”监管");
            } else if(apJgxwCheckAction.getCheckMode().equals("2")) {
                row.add("重点监管");
            } else if (apJgxwCheckAction.getCheckMode().equals("3")) {
                row.add("信用监管");
            }
            if (apJgxwCheckAction.getCheckResult().equals("1")) {
                row.add("未发现问题终止检查并向监管对象告知检查结果");
            } else if (apJgxwCheckAction.getCheckResult().equals("2")) {
                row.add("发现问题作出责令改正等行政命令");
            } else if (apJgxwCheckAction.getCheckResult().equals("3")) {
                row.add("发现问题做出行政指导");
            } else if (apJgxwCheckAction.getCheckResult().equals("4")) {
                row.add("发现问题作出行政处罚决定");
            } else if (apJgxwCheckAction.getCheckResult().equals("5")) {
                row.add("发现问题作出行政强制决定");
            } else if (apJgxwCheckAction.getCheckResult().equals("6")) {
                row.add("发现问题作出其他具体行政行为");
            }else {
                row.add(apJgxwCheckAction.getCheckResult());
            }
            SimpleDateFormat spf = new SimpleDateFormat("yyyy-MM-dd");
            String formaTime = spf.format(apJgxwCheckAction.getCheckDate());
            row.add(formaTime);
            rows.add(row);
        }
        data.setRows(rows);
        try {
            // 导出
            ExportExcelUtils.exportExcel(request,response, "行政检查行为监管表.xlsx", data);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    @GetMapping("/getAllNum")
    private R getNum(){
        String other = HttpClientUtil.doGet(AP_URL + "/other/getTotalCount");
        String force = HttpClientUtil.doGet(AP_URL + "/force/forceCount");
        String check = HttpClientUtil.doGet(AP_URL + "/check/num");
        String punish = HttpClientUtil.doGet(AP_URL + "/punish/countNum");
        int obOther = 0;
        int obForce = 0;
        int obCheck = 0;
        int obPunish = 0;
        int sum = 0;
        if(!StringUtils.isEmpty(other)){
            obOther = Integer.parseInt(JSON.parse(other).toString());
        }
        if(!StringUtils.isEmpty(force)){
            JSONObject jsonObject=(JSONObject)JSON.parse(force);
            obForce = Integer.parseInt(jsonObject.get("data").toString());
        }
        if(!StringUtils.isEmpty(check)){
            obCheck = Integer.parseInt(JSON.parse(check).toString());
        }
        if(!StringUtils.isEmpty(punish)){
            obPunish = Integer.parseInt(JSON.parse(punish).toString());
        }
        sum=obOther+obForce+obCheck+obPunish;
        return R.ok().put("sum",sum).put("obOther",obOther).put("obForce",obForce).put("obCheck",obCheck).put("obPunish",obPunish);
    }
}
